/* tslint:disable: no-unused-expression */
import * as zmq from "../../src"

import {assert} from "chai"
import {createProcess} from "./helpers"

describe("context process exit", function() {
  describe("with default context", function() {
    it("should occur when sockets are closed", async function() {
      this.slow(200)
      const code = await createProcess(() => {
        const socket1 = new zmq.Dealer
        socket1.close()
        const socket2 = new zmq.Router
        socket2.close()
      })

      assert.equal(code, 0)
    })

    it("should occur when sockets are not closed", async function() {
      this.slow(200)
      const code = await createProcess(() => {
        const socket1 = new zmq.Dealer
        const socket2 = new zmq.Router
      })

      assert.equal(code, 0)
    })

    it("should not occur when sockets are open and polling", async function() {
      this.slow(1000)
      const code = await createProcess(() => {
        const socket1 = new zmq.Dealer
        socket1.connect("inproc://foo")
        socket1.receive()
      })

      assert.equal(code, -1)
    })
  })

  describe("with custom context", function() {
    it("should occur when sockets are closed", async function() {
      this.slow(200)
      const code = await createProcess(() => {
        const context = new zmq.Context
        const socket1 = new zmq.Dealer({context})
        socket1.close()
        const socket2 = new zmq.Router({context})
        socket2.close()
      })

      assert.equal(code, 0)
    })

    it("should occur when sockets are closed and context is gced", async function() {
      this.slow(200)
      const code = await createProcess(() => {
        function run() {
          const context = new zmq.Context
          const socket1 = new zmq.Dealer({context})
          socket1.close()
          const socket2 = new zmq.Router({context})
          socket2.close()
        }

        run()
        global.gc()
      })

      assert.equal(code, 0)
    })

    it("should occur when sockets are not closed", async function() {
      this.slow(200)
      const code = await createProcess(() => {
        const context = new zmq.Context
        const socket1 = new zmq.Dealer({context})
        const socket2 = new zmq.Router({context})
      })

      assert.equal(code, 0)
    })

    it("should not occur when sockets are open and polling", async function() {
      this.slow(1000)
      const code = await createProcess(() => {
        const context = new zmq.Context
        const socket1 = new zmq.Dealer({context})
        socket1.connect("inproc://foo")
        socket1.receive()
      })

      assert.equal(code, -1)
    })
  })
})
